///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_APP_CMESSAGEQUEUE_H__
#define __BUOLA_APP_CMESSAGEQUEUE_H__

#include <buola/buola.h>
#include <vector>
#include <buola/app/ceventloop.h>

namespace buola {

///\ingroup application signals
///@{

///////////////////////////////////////////////////////////////////////////

/// message queue

/// This class stores events which have been triggered, and dispatches them
/// when the loop function is called for each iteration. Only one object of
/// this class will be created, from CApp, as the event set of classes
/// always use the message queue from the gApp object.
///
///////////////////////////////////////////////////////////////////////////

class CMessageQueue : public CLoopFunction
{
public:
//! \name constructor and destructor
//@{
    CMessageQueue(CEventLoop &pEventLoop);
    virtual ~CMessageQueue();
//@}

public:
    //! stores a triggered event to be dispatched in the next iteration
    void StoreTriggered(triggered_event *pEvent);
private:
    //! called every iteration, it dispatches stored events
    virtual bool OnLoopFunction();

    mutex mMutex;
    int mTriggeredPending;
    std::vector<triggered_event*> mTriggeredPool;
};

///returns a reference to the default message queue
CMessageQueue &get_main_message_queue();

///@}

/*namespace buola*/ }

#endif
